package com.tech.doggtv.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.tech.doggtv.R;
import com.tech.doggtv.action.EventType;
import com.tech.doggtv.action.OnItemClickListener;
import com.tech.doggtv.adapter.ColumnsAdapter;
import com.tech.doggtv.adapter.ProgsAdapter;
import com.tech.doggtv.comm.SDCardFileUtils;

import java.io.File;

import butterknife.InjectView;
import de.greenrobot.event.EventBus;

/**
 * @author jl_luo
 * @name: com.tech.doggtv.fragment
 * @description:
 * @date 2015/9/14 16:23
 */

public class ProgramsListFragment extends BaseFragment{
    private static final String TAG = ProgramsListFragment.class.getName();
    @InjectView(R.id.rv_columns)
    RecyclerView list;
    private String progType = "1";
    File[] progs ;
    public static ProgramsListFragment newInstance(String num) {
        ProgramsListFragment fragment = new ProgramsListFragment();
        Bundle args = new Bundle();
        args.putString("num", num);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.recycleview_list);
        list.setLayoutManager(new LinearLayoutManager(getActivity()));
        Log.v(TAG, "~~~~~~~~~~~~~~~~~");
        initData();
    }

    @Override
    protected void onUserVisible() {
        Log.v(TAG, "~~~~~~~~~~~~~~~~~onUserVisible=" + getUserVisibleHint());
    }
    private void initData(){
        progType = getArguments().getString("num");
        File file = SDCardFileUtils.getSDCardVideoPath(progType);
        progs = file.listFiles();
        Log.v(TAG,"~~~~~~~~~~~~~~~~~progs.length="+progs.length);
        ProgsAdapter columnsAdapter =  new ProgsAdapter(progs);
        list.setAdapter(columnsAdapter);
        columnsAdapter.notifyDataSetChanged();
        columnsAdapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                Log.v(TAG,"~~~~~~~~~~~~~~~111=="+pos);
                EventType eventType = new EventType(progs,pos);
                EventBus.getDefault().post(eventType);
                getActivity().finish();
            }
        });
    }


}
